package flc.ast.fragment.date;

import android.view.View;

import androidx.fragment.app.Fragment;

import com.blankj.utilcode.util.ToastUtils;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.core.BasePopupView;
import com.lxj.xpopup.enums.PopupPosition;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import flc.ast.R;
import flc.ast.databinding.FragmentDateCalBinding;
import flc.ast.dialog.CalTypeDialog;
import flc.ast.dialog.SelDateDialog;
import stark.common.basic.base.BaseNoModelFragment;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link DateCalFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class DateCalFragment extends BaseNoModelFragment<FragmentDateCalBinding> {

    private SelDateDialog selDateDialog;
    private Date dateTime;
    private BasePopupView calTypePopup;
    private CalTypeDialog calTypeDialog;
    private int type;

    @Override
    protected int onCreate() {
        return R.layout.fragment_date_cal;
    }

    @Override
    protected void initView() {
        mDataBinding.llStartDate.setOnClickListener(this);
        mDataBinding.tvCalType.setOnClickListener(this);
        mDataBinding.tvStartCal.setOnClickListener(this);

        Date currentDate = new Date(System.currentTimeMillis());
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        dateTime = currentDate;
        mDataBinding.tvStartDate.setText(dateFormat.format(currentDate));
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.llStartDate://开始日期
                showSelDateDialog();
                break;
            case R.id.tvCalType://向前推算/向后推算
                showCalTypeDialog();
                break;
            case R.id.tvStartCal://开始推算
                calculate();
                break;
        }
    }

    @Override
    protected void initData() {

    }

    private void calculate() {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(dateTime);
        if (mDataBinding.etDateCalNum.getText().toString().isEmpty()) {
            ToastUtils.showShort(getString(R.string.please_input_day));
            return;
        }
        int distance = type == 0 ? -Integer.parseInt(mDataBinding.etDateCalNum.getText().toString()) : Integer.parseInt(mDataBinding.etDateCalNum.getText().toString());
        calendar.set(Calendar.DATE, calendar.get(Calendar.DATE) + distance);
        mDataBinding.tvDateCalRet.setText(dateFormat.format(calendar.getTime()));
    }

    //选择日期弹框
    private void showSelDateDialog() {
        if (selDateDialog == null) {
            selDateDialog = new SelDateDialog(mContext);
        }
        selDateDialog.title = getString(R.string.start_date);
        selDateDialog.setListener(new SelDateDialog.Ilistener() {
            @Override
            public void selDate(int year, int month, int day) {
                String selDate = year + "-" + (month < 10 ? "0" + month : month) + "-" + (day < 10 ? "0" + day : day);
                SimpleDateFormat stringFormat = new SimpleDateFormat("yyyy-MM-dd");
                try {
                    Date date = stringFormat.parse(selDate);
                    dateTime = date;
                } catch (ParseException e) {
                    throw new RuntimeException(e);
                }
                mDataBinding.tvStartDate.setText(selDate);
            }
        });
        selDateDialog.show();
    }

    //向前/向后推算
    private void showCalTypeDialog() {
        if (calTypeDialog == null) calTypeDialog = new CalTypeDialog(mContext);
        calTypePopup = new XPopup.Builder(mContext).popupPosition(PopupPosition.Bottom) //手动指定弹窗的位置
                .isDestroyOnDismiss(false) //对于只使用一次的弹窗，推荐设置这个
                .atView(mDataBinding.tvCalType).hasShadowBg(false) // 去掉半透明背景
                .asCustom(calTypeDialog);
        calTypeDialog.setListener(new CalTypeDialog.Ilistener() {
            @Override
            public void click(int pos) {
                if (pos == 0) {
                    mDataBinding.tvCalType.setText(getString(R.string.forward_cal));
                } else {
                    mDataBinding.tvCalType.setText(getString(R.string.backward_cal));
                }
                type = pos;
                calTypePopup.dismiss();
            }
        });
        calTypePopup.show();
    }

}